package com.power.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.power.domain.IndexImg;
import com.power.domain.Prod;
import com.power.ex.handle.BussinessException;
import com.power.fegin.StoreProdFeign;
import com.power.mapper.IndexImgMapper;
import com.power.model.Result;
import com.power.service.IndexImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
* @author dingchunlin
* @description 针对表【index_img(主页轮播图)】的数据库操作Service实现
* @createDate 2024-07-10 15:12:13
*/
@Service
@CacheConfig(cacheNames = "com.power.service.IndexImgService")
public class IndexImgServiceImpl extends ServiceImpl<IndexImgMapper, IndexImg> implements IndexImgService{

    @Autowired
    private IndexImgMapper indexImgMapper;

    @Autowired
    private StoreProdFeign storeProdFeign;


    /**
     * 保存主页轮播图
     * @param indexImg 主页轮播图
     * @return Boolean
     */
    @Override
    @CacheEvict(key = "'queryWxIndexImgsList'")
    public Boolean saveIndexImg(IndexImg indexImg) {
        indexImg.setShopId(1L);
        indexImg.setCreateTime(new Date());
        // 获取关联类型
        Integer type = indexImg.getType();
        // 判断关联类型
        if ( -1 == type){
            indexImg.setProdId(-1L);
        }
        return indexImgMapper.insert(indexImg) > 0;
    }

    /**
     * 根据标识查询轮播图
     * @param id 轮播图标识
     * @return 轮播图
     */
    @Override
    public IndexImg loadIndexImgInfo(Long id) {
        // 根据标识查询轮播图
        IndexImg indexImg = indexImgMapper.selectById(id);
        // 获取轮播图类型
        Integer type = indexImg.getType();
        // 判断轮播图类型
        if ( 0 == type){
            Long prodId = indexImg.getProdId();
            // 根据商品ID查询商品信息
            Result<List<Prod>> result = storeProdFeign.getProdListByIds(Arrays.asList(prodId));
            if (!result.getCode().equals(200)){
                throw new BussinessException("获取商品信息失败");
            }
            //获取数据
            List<Prod> prods = result.getData();
            if (CollectionUtils.isNotEmpty(prods) && prods.size() > 0){
                Prod prod = prods.get(0);
                indexImg.setProdName(prod.getProdName());
                indexImg.setPic(prod.getPic());
            }

        }
        // 返回轮播图
        return indexImg;
    }

    /**
     * 修改轮播图
     *
     * @param indexImg 轮播图
     * @return Boolean
     */
    @Override
    @CacheEvict(key = "'queryWxIndexImgsList'")
    public Boolean modifyIndexImg(IndexImg indexImg) {
        return indexImgMapper.updateById(indexImg) > 0;
    }

    /**
     * 根据图片id删除轮播图
     * @param imageIds 图片id集合
     * @return Boolean
     */
    @Override
    @CacheEvict(key = "'queryWxIndexImgsList'")
    public Boolean removeIndexImgByImageIds(List<Long> imageIds) {
        // 根据图片id删除轮播图
        return indexImgMapper.deleteBatchIds(imageIds) == imageIds.size();
    }

    /**
     * 查询微信端首页轮播图列表
     * @return 轮播图列表
     */
    @Override
    @Cacheable(key = "'queryWxIndexImgsList'")
    public List<IndexImg> queryWxIndexImgsList() {
        return indexImgMapper.selectList(new LambdaQueryWrapper<IndexImg>()
               .eq(IndexImg::getStatus, 1)
               .orderByDesc(IndexImg::getSeq));
    }
}




